// JavaScript Document
var calendar=function(){
	var dateFormat="MM/dd/yyyy";
	var defaultDate=(new Date()).format(dateFormat);
	var minYear=1970;
	var maxYear=2099;
	var curYear;
	var curMonth;
	var curDay;
	var curLang;
	var curSelectedDate;
	var en_us={week:['Sun','Mon','Tue','Wed','Thu','Fri','Sat']};
	var shtml=[];
	var dayitem='cursor:pointer;color: #000000;';
	var dayitem_no='cursor:pointer;color: #999999;';
	var dayitem_cur='cursor:pointer;background-color:#FFC;';
	var dayitem_dis='text-decoration:line-through;';
	var dayitem_sel='border:#F00 1px solid;';
	var defaultoption={mindate:'01/01/'+1970,maxdate:'12/31/'+maxYear,dateformat:dateFormat,defaultdate:defaultDate,field:null,language:en_us}
	function creatediv(option){
		var csstxt;
		var calDIV;
		
		calDIV=document.createElement('div');
		calDIV.id='__calendarDIV';
		csstxt='font-family: "Verdana", "Arial", "Helvetica", "sans-serif";font-size: 12px;color: #000000;border:#999999 1px solid;position:absolute;filter:alpha(opacity=100);opacity:1;z-index:999999;display:none;background-color:#ffffff;width:210px;overflow:visible;';
		if(!+"\v1"){
			//use the .cssText hack
		calDIV.style.setAttribute('cssText',csstxt);
		}else{
			calDIV.setAttribute('style',csstxt);
		}
		createheader(option);
		createbody();
		createfooter();
		calDIV.innerHTML=shtml.join('');
		shtml=[];
		return calDIV;
	}
	function createheader(option){
		shtml.push('<table width="210" border="0" cellspacing="0" cellpadding="0" id="__dayheader">');
		shtml.push('<tr>')
		shtml.push('<td width="25" height="25" align="center" id="___PREVYEAR" style="cursor:pointer;">&lt;&lt;</td>')
		shtml.push('<td width="25" height="25" align="center" id="___PREVMONTH" style="cursor:pointer;">&lt;</td>')
		shtml.push('<td height="25" align="center" nowrap><span  id="__calendar_year_txt" style="cursor:pointer;"></span><span id="__calendar_year_list" style="display:none;"><select id="__YEARLIST"></select></span></td>')
		shtml.push('<td height="25" align="center" nowrap  width="30"><span  id="__calendar_month_txt" style="cursor:pointer;"></span><span  id="__calendar_month_list"  style="display:none;"><select id="__MONTHLIST"></select></span></td>')
		shtml.push('<td width="25" height="25" align="center" id="___NEXTMONTH" style="cursor:pointer;">&gt;</td>')
		shtml.push('<td width="25" height="25" align="center" id="___NEXTYEAR" style="cursor:pointer;">&gt;&gt;</td>')
		shtml.push('</tr>')
		shtml.push('</table>')
	}
	function createbody(){
		var curDate=new Date(curMonth+"/01"+"/"+curYear);
		var curWeek=curDate.getDay();
		var beginDate=new Date(curDate.setDate(curDate.getDate()-curWeek-1))
		shtml.push('<table border="0" cellspacing="1" cellpadding="0" id="__daylist">')
		shtml.push('<tr>')
		if(typeof curLang.week!=='object'){
			curLang=en_us;
		}
		for(var i=0;i<curLang.week.length;i++){
			shtml.push('<td width="30" height="25" align="center">'+curLang.week[i]+'</td>');
		}
		shtml.push('</tr>')
		for(var row=0;row<6;row++){
			shtml.push('<tr>')
			for(var col=0;col<7;col++){
				var index=(row*7)+col
				var realdate=new Date((beginDate.setDate(beginDate.getDate()+1)));
				var realday=realdate.getDate();
				var realmonth=realdate.getMonth()+1
				var dateclass=(realmonth==curMonth)?dayitem:dayitem_no;
				if(realdate.format(dateFormat)===(new Date()).format(dateFormat))dateclass=dayitem_cur;
				if(curSelectedDate&&realdate.format(dateFormat)===(new Date(curSelectedDate)).format(dateFormat))dateclass+=dayitem_sel;
				var date_lab=realdate.format(dateFormat);
				if(realdate.getFullYear()>maxYear||realdate.getFullYear()<minYear){
					dateclass+=dayitem_dis;
					date_lab='';
				}
				shtml.push('<td width="30" style="'+dateclass+'" height="25" date="'+date_lab+'" id="day'+index+'" align="center">'+realday+'</td>');
			}
			shtml.push('</tr>')
		}
		shtml.push('</table>')
	}
	function createfooter(){
		shtml.push('<table width="210" border="0" cellspacing="0" cellpadding="0" id="__dayfooter">')
		shtml.push('<tr>')
		shtml.push('<td height="25" align="right"><span style="cursor:pointer;" id="___TODAY">Today</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="cursor:pointer;" id="___CLOSE">Close</span>&nbsp;&nbsp;</td>')
		shtml.push('</tr>')
		shtml.push('</table>')
	}
	function settoday(option){
		option.field.value=defaultDate;
	}
	function makecalendar(option){
		document.body.appendChild(creatediv(option));
		showcurYear();
		showcurMonth();
		updatedaylist();
		bindevent(option);
	}
	function bindevent(option){
		Fjs.addEvent(Fjs.getId('__daylist'),'click',function(e){
			var selday=Fjs.getEventTarget(e).getAttribute('date')
			if(selday){
				option.field.value=selday;
				closecalendar();	
			}
			Fjs.stopPropagation(e);
		})
		Fjs.addEvent(document.body,'click',function(e){
			closecalendar();	
		})
		Fjs.addEvent(window,'click',function(e){
			closecalendar();	
		})
		Fjs.addEvent(Fjs.getId('__dayheader'),'click',function(e){
			
			var clobj=Fjs.getEventTarget(e).getAttribute('id')
			
			if(clobj==='___NEXTYEAR'){
				nextyear();
			}else if(clobj==='___PREVYEAR'){
				prevyear();
			}else if(clobj==='___PREVMONTH'){
				prevmonth();
			}else if(clobj==='___NEXTMONTH'){
				nextmonth();
			}else if(clobj==='__calendar_year_txt'){
				showyearlist();
			}else if(clobj==='__calendar_month_txt'){
				showmonthlist();
			}
			Fjs.stopPropagation(e);	
		})
		Fjs.addEvent(Fjs.getId('__dayfooter'),'click',function(e){
			var clobj=Fjs.getEventTarget(e).getAttribute('id')
			closeselect();
			if(clobj==='___TODAY'){
				settoday(option);
				closecalendar();
			}else if(clobj==='___CLOSE'){
				closecalendar();
			}
			Fjs.stopPropagation(e);	
		})
		Fjs.addEvent(Fjs.getId('__YEARLIST'),'change',function(e){
			closeselectyear();
		});
		
		Fjs.addEvent(Fjs.getId('__MONTHLIST'),'change',function(e){
			closeselectmonth()
		});
	}
	function closeselect(){
		closeselectmonth();
		closeselectyear();
	}
	function closeselectyear(){
		var objsel=Fjs.getId('__YEARLIST');
		var objlist=Fjs.getId('__calendar_year_list');
		if(objlist.style.display!='none')curYear=parseInt(objsel.options[objsel.options.selectedIndex].value,10);
		Fjs.getId('__calendar_year_txt').style.display='';
		objlist.style.display='none';
		updatecalaner();
	}
	function closeselectmonth(){
		var objsel=Fjs.getId('__MONTHLIST')
		var objlist=Fjs.getId('__calendar_month_list');
		if(objlist.style.display!='none')curMonth=parseInt(objsel.options[objsel.options.selectedIndex].value,10);
		Fjs.getId('__calendar_month_txt').style.display='';
		objlist.style.display='none';
		updatecalaner();
	}
	function showyearlist(){
		var yearlist=Fjs.getId('__YEARLIST');
		var cyear;
		var beginIndex;
		var EndIndex;
		var yearstep=20;
		closeselectmonth();
		beginIndex=-((yearstep)/2);
		EndIndex=beginIndex+yearstep
		Fjs.getId('__calendar_year_txt').style.display='none';
		Fjs.getId('__calendar_year_list').style.display='';
		yearlist.length=0;
		if((curYear+beginIndex)<=minYear){
			beginIndex=minYear-curYear;
			EndIndex=beginIndex+yearstep;	
		}
		if((curYear+EndIndex)>=maxYear){
			EndIndex=maxYear-curYear;
			beginIndex=EndIndex-yearstep;	
		}
		for(var i=beginIndex;i<=EndIndex ;i++){
			cyear=curYear+i;
			if(parseInt(cyear,10)<=maxYear&&parseInt(cyear,10)>=minYear){
				Fjs.selectHTML.add(yearlist,cyear,cyear);
			}
		}
		Fjs.selectHTML.setDefalut(yearlist,curYear);
	}
	function showmonthlist(){
		var monthlist=Fjs.getId('__MONTHLIST');
		closeselectyear();
		Fjs.getId('__calendar_month_txt').style.display='none';
		Fjs.getId('__calendar_month_list').style.display='';
		monthlist.length=0;
		for(var i=1;i<=12;i++){
			Fjs.selectHTML.add(monthlist,i,i);
		}
		Fjs.selectHTML.setDefalut(monthlist,curMonth);
	}
	function createiframe(){
		var calIFRM=document.createElement('iframe');
		calIFRM.id='__calendarIFRM';
		calIFRM.setAttribute('frameBorder','0');
		var csstxt='position:absolute;filter:alpha(opacity=0);opacity:0;z-index:99999;display:none;';
		if(!+"\v1"){
			calIFRM.style.setAttribute('cssText',csstxt)
		}else{
			calIFRM.setAttribute('style',csstxt);
		}
		document.body.appendChild(calIFRM);
	}
	function createcalendar(obj,option){
		if(option.field===null) return; 
		var calIFRM=Fjs.getId('__calendarIFRM');
		var calDiv=Fjs.getId('__calendarDIV');
		if(!calIFRM){
			createiframe();
		}
		if(!calDiv){
			makecalendar(option);
		}
	}
	function updatedaylist(){
		var curDate;
		var curWeek;
		var beginDate;
		var realdate;
		var realday;
		var realmonth;
		var dateclass;
		curDate=new Date(curMonth+"/01"+"/"+curYear);
		curWeek=curDate.getDay();
		if(curWeek==0){
			beginDate=new Date(curDate.setDate(curDate.getDate()-curWeek-8))
		}else{
			beginDate=new Date(curDate.setDate(curDate.getDate()-curWeek-1))
		}
		for(var i=0;i<42;i++){
			realdate=new Date((beginDate.setDate(beginDate.getDate()+1)));
			realday=realdate.getDate();
			realmonth=realdate.getMonth()+1
			dateclass=(realmonth==curMonth)?dayitem:dayitem_no;
			if(realdate.format(dateFormat)===(new Date()).format(dateFormat)) dateclass=dayitem_cur;
			if(curSelectedDate&&realdate.format(dateFormat)===(new Date(curSelectedDate)).format(dateFormat))dateclass+=dayitem_sel;
			var date_lab=realdate.format(dateFormat);
			if(realdate.getFullYear()>maxYear||realdate.getFullYear()<minYear){
				dateclass+=dayitem_dis;
				date_lab='';
			}
			(function(s){
				var curcell=Fjs.getId('day'+s);
				curcell.innerHTML=realday;
				curcell.setAttribute('date',date_lab);
				if(!+"\v1"){
					curcell.style.setAttribute('cssText',dateclass)
				}else{
					curcell.setAttribute('style',dateclass);
				}
			})(i)
		}
	}
	function updatecalaner(){
		showcurYear();
		showcurMonth();
		updatedaylist();
	}
	function showcurYear(){
		Fjs.getId('__calendar_year_txt').innerHTML=curYear;
	}
	function showcurMonth(){
		Fjs.getId('__calendar_month_txt').innerHTML=curMonth;
	}
	function nextyear(){
		closeselect();
		if(parseInt(curYear,10)>=parseInt(maxYear,10)) return;
		curYear++
		showcurYear();
		updatedaylist();
	}
	function prevyear(){
		closeselect();
		if(parseInt(curYear,10)<=parseInt(minYear,10)) return;
		curYear--
		showcurYear();
		updatedaylist();
	}
	function prevmonth(){
		closeselect();
		if((parseInt(curMonth,10)-1)<1){
			curYear--;
			curMonth=12;
			showcurYear();
		}else{
			curMonth--;
		}
		showcurMonth();
		updatedaylist();
	}
	function nextmonth(){
		closeselect();
		if((parseInt(curMonth,10)+1)>12){
			curYear++;
			curMonth=1;
			showcurYear();
		}else{
			curMonth++;
		}
		showcurMonth();
		updatedaylist();
	}
	function calendaroption(option){
		//{mindate:'01/01/'+1970,maxdate:'12/31/'+maxYear,dateformat:dateFormat,defaultdate:defaultDate,field:null,language:zh_cn}
		var opt=option||defaultoption;
		if(option){
			opt.mindate=opt.mindate||defaultoption.mindate;
			opt.maxdate=opt.maxdate||defaultoption.maxdate;
			opt.dateformat=opt.dateformat||defaultoption.dateformat;
			opt.defaultdate=opt.defaultdate||defaultoption.defaultdate;
			opt.field=opt.field||defaultoption.field;
			opt.language=opt.language||defaultoption.language;			
		}
		minYear=parseInt(new Date(opt.mindate).getFullYear(),10);
		maxYear=parseInt(new Date(opt.maxdate).getFullYear(),10);
		curLang=opt.language;
		curSelectedDate=(opt.field?opt.field.value:null);
		return opt;
	}
	function showcalendar(obj,option){
		var opt;
		var nTop,nLeft;
		var curdate;
		if(!obj) return;
		if(Fjs.getId('__calendarDIV')) closecalendar();
		opt=calendaroption(option);
		if(opt.field){
			curdate=opt.field.value;
		}
		curdate=curdate||opt.defaultdate;
		curdate=new Date(curdate)
		curYear=curdate.getFullYear();
		curMonth=curdate.getMonth()+1;
		nTop=Fjs.getAbsPosition(obj).top+'px';
		nLeft=(Fjs.getAbsPosition(obj).left+obj.offsetWidth)+'px';
		createcalendar(obj,opt);
		displaycalendar(nTop,nLeft);
	}
	function displaycalendar(top,left){
		var ifrm,idiv;
		ifrm=Fjs.getId('__calendarIFRM');
		idiv=Fjs.getId('__calendarDIV');
		idiv.style.display='';
		idiv.style.top=top
		idiv.style.left=left;
		ifrm.style.display='';
		ifrm.style.height=idiv.offsetHeight+'px';
		ifrm.style.width=idiv.offsetWidth+'px';
		ifrm.style.top=top;
		ifrm.style.left=left;
	}
	function closecalendar(){
		ifrm=Fjs.getId('__calendarIFRM');
		idiv=Fjs.getId('__calendarDIV');
		(idiv)&&(document.body.removeChild(idiv));
		(ifrm)&&(document.body.removeChild(ifrm));
	}
	return{
		show:showcalendar,
		close:closecalendar
	}
}();
